{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-21T05:21:35.654733Z",
     "start_time": "2025-09-21T05:21:24.951006Z"
    }
   },
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')\n",
    "\n",
    "element = wd.find_element(By.ID, 'kw')\n",
    "element.send_keys('通讯\\n')\n",
    "\n",
    "input('按回车退出')"
   ],
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 2
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "**根据ClassName选择元素**",
   "id": "2848a96a3f274cc5"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T05:32:09.881723Z",
     "start_time": "2025-09-21T05:31:58.377915Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "elements = wd.find_elements(By.CLASS_NAME, 'animal')\n",
    "\n",
    "for element in elements:\n",
    "    print(element.text)\n",
    "\n",
    "input('按回车退出')"
   ],
   "id": "9b720714a1f69ed7",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "狮子\n",
      "老虎\n",
      "山羊\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T05:34:18.872534Z",
     "start_time": "2025-09-21T05:34:09.867682Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "# 根据 tag name 选择元素，返回的是 一个列表\n",
    "# 里面 都是 tag 名为 div 的元素对应的 WebElement对象\n",
    "elements = wd.find_elements(By.TAG_NAME, 'div')\n",
    "\n",
    "# 取出列表中的每个 WebElement对象，打印出其text属性的值\n",
    "# text属性就是该 WebElement对象对应的元素在网页中的文本内容\n",
    "for element in elements:\n",
    "    print(element.text)\n",
    "\n",
    "input('按回车退出')"
   ],
   "id": "66dee7b7293958c2",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "土豆\n",
      "洋葱\n",
      "白菜\n",
      "狮子\n",
      "老虎\n",
      "山羊\n",
      "张三\n",
      "内层11\n",
      "内层12\n",
      "内层21\n",
      "内层11\n",
      "内层12\n",
      "内层11\n",
      "内层12\n",
      "内层21\n",
      "内层21\n",
      "版权 发布日期：2018-03-03\n",
      "备案号 苏ICP备88885574号\n",
      "版权 发布日期：2018-03-03\n",
      "None\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T05:39:05.387629Z",
     "start_time": "2025-09-21T05:38:59.079727Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/sample1.html')\n",
    "\n",
    "element = wd.find_element(By.ID,'container')\n",
    "\n",
    "# 限制 选择元素的范围是 id 为 container 元素的内部。\n",
    "spans = element.find_elements(By.TAG_NAME, 'span')\n",
    "for span in spans:\n",
    "    print(span.text)\n",
    "\n",
    "input('按回车退出')"
   ],
   "id": "8721ad67ebe61d3b",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "内层11\n",
      "内层12\n",
      "内层21\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T05:41:52.436329Z",
     "start_time": "2025-09-21T05:41:43.414539Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 等待界面元素的出现\n",
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')\n",
    "\n",
    "element = wd.find_element(By.ID, 'kw')\n",
    "\n",
    "element.send_keys('通讯\\n')\n",
    "\n",
    "from time import sleep\n",
    "sleep(1)\n",
    "\n",
    "# 返回页面 ID为1 的元素\n",
    "element = wd.find_element(By.ID,'1')\n",
    "# 打印该元素的文字内容\n",
    "print(element.text)\n",
    "\n",
    "input('按回车退出')"
   ],
   "id": "af3d59da2744d941",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "国美通讯\n",
      "代码：600898\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-21T05:47:08.871239Z",
     "start_time": "2025-09-21T05:46:59.163933Z"
    }
   },
   "cell_type": "code",
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.common.by import By\n",
    "\n",
    "wd = webdriver.Chrome()\n",
    "wd.implicitly_wait(10)\n",
    "\n",
    "wd.get('https://www.byhy.net/cdn2/files/selenium/stock1.html')\n",
    "\n",
    "element = wd.find_element(By.ID, 'kw')\n",
    "\n",
    "element.send_keys('通讯\\n')\n",
    "\n",
    "\n",
    "# 返回页面 ID为1 的元素\n",
    "element = wd.find_element(By.ID,'1')\n",
    "\n",
    "print(element.text)\n",
    "\n",
    "input('按回车退出')"
   ],
   "id": "ff976c2d6db041d0",
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Exception managing chrome: error sending request for url (https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "国美通讯\n",
      "代码：600898\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "''"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "c059c681d4cb4367"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
